#pragma once

#include <functional>
#include <string>
#include <iostream>

#define option "+-*/%"

class Task
{
public:
    // 设置一个函数对象
    typedef std::function<int(int, int, char)> func_t;

public:
    Task()
    {
    }

    Task(int x, int y, char op, func_t callback)
        : _x(x), _y(y), _op(op), _callback(callback)
    {
    }

    // 供消费者调用，打印任务结果
    std::string operator()()
    {
        int result = _callback(_x, _y, _op);
        char buf[1024];
        snprintf(buf, sizeof(buf), "%d %c %d = %d", _x, _op, _y, result);
        return buf;
    }

    // 供生产者调用，打印任务
    std::string taskToString()
    {
        char buf[1024];
        snprintf(buf, sizeof(buf), "%d %c %d = ?", _x, _op, _y);
        return buf;
    }

private:
    int _x;
    int _y;
    char _op; //+-*/%, 选择哪个运算
    func_t _callback;
};

int mymath(int x, int y, char op)
{
    int result = 0;
    switch (op)
    {
    case '+':
        result = x + y;
        break;
    case '-':
        result = x - y;
        break;
    case '*':
        result = x * y;
        break;
    case '/':
    {
        if (y == 0)
        {
            std::cout << "div zero error" << std::endl;
            result = -1;
        }
        else
        {
            result = x / y;
        }
    }
    break;
    case '%':
    {
        if (y == 0)
        {
            std::cout << "mod zero error" << std::endl;
            result = -1;
        }
        else
        {
            result = x % y;
        }
    }
    break;

    default:
        break;
    }

    return result;
}

